package com.huadao.cart_ccs.experiment.cleanPipes;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.ParamsPreferences;
import com.huadao.cart_ccs.data.task.CleanPipesTask;

import butterknife.BindView;
import butterknife.OnClick;

public class CleanPipesDF extends BaseDialogFragment {

    public interface OnCleanPipes {
        void cleanPipes(int fluidAmount, int wasteAmount, int cleanNumbers, int loopTime, int fluidSpeed, int circlingSpeed, int wasteSpeed);

        void terminateCleanPipes();
    }

    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.et_fluid_amount)
    EditText etFluidAmount;
    @BindView(R.id.et_fluid_pump)
    EditText etFluidPump;
    @BindView(R.id.et_loop_times)
    EditText etLoopTimes;
    @BindView(R.id.et_circling_pump)
    EditText etCirclingPump;
    @BindView(R.id.et_waste_pump)
    EditText etWastePump;
    @BindView(R.id.et_clean_numbers)
    EditText etCleanNumbers;
    @BindView(R.id.btn_stop_operate)
    Button btnStopOperate;
    @BindView(R.id.btn_operate)
    Button btnOperate;

    private CleanPipesTask cleanPipesTask;
    private OnCleanPipes onCleanPipes;

    public static CleanPipesDF newInstance(CleanPipesTask cleanPipesTask) {
        CleanPipesDF cleanPipesDF = new CleanPipesDF();
        Bundle bundle = new Bundle();
        bundle.putSerializable("cleanPipesTask", cleanPipesTask);
        cleanPipesDF.setArguments(bundle);
        return cleanPipesDF;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_clean_pipes;
    }

    @Override
    protected void onInit() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            cleanPipesTask = (CleanPipesTask) bundle.get("cleanPipesTask");
        }
        this.getDialog().setCanceledOnTouchOutside(false);
        etFluidPump.setEnabled(false);
        etWastePump.setEnabled(false);
        etCirclingPump.setEnabled(false);
        if (cleanPipesTask != null) {
            etFluidAmount.setText(String.valueOf(cleanPipesTask.getFluidAmount()));
            etFluidPump.setText(String.valueOf(cleanPipesTask.getFluidSpeed()));
            etLoopTimes.setText(String.valueOf(cleanPipesTask.getLoopTime() / 60));
            etCirclingPump.setText(String.valueOf(cleanPipesTask.getCirclingSpeed()));
            etWastePump.setText(String.valueOf(cleanPipesTask.getWasteSpeed()));
            etCleanNumbers.setText(String.valueOf(String.valueOf(cleanPipesTask.getCleanNumbers())));
            etFluidAmount.setEnabled(false);
            etLoopTimes.setEnabled(false);
            etCleanNumbers.setEnabled(false);
            btnOperate.setText("清洗中...");
            btnOperate.setEnabled(false);
            btnStopOperate.setVisibility(View.VISIBLE);
        } else {
            etFluidPump.setText(String.valueOf(ParamsPreferences.getInstance().getFluidIntakePumpSpeed()));
            etWastePump.setText(String.valueOf(ParamsPreferences.getInstance().getWastePumpSpeed()));
            etCirclingPump.setText(String.valueOf(ParamsPreferences.getInstance().getCirculatingPumpSpeed()));
            btnOperate.setText("清洗");
            btnStopOperate.setVisibility(View.GONE);
        }
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @OnClick({R.id.btn_close, R.id.btn_stop_operate, R.id.btn_operate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                this.dismiss();
                break;
            case R.id.btn_stop_operate:
                if (onCleanPipes != null) {
                    onCleanPipes.terminateCleanPipes();
                }
                this.dismiss();
                break;
            case R.id.btn_operate:
                String err = check();
                if (err != null) {
                    showDialogMessage("", err);
                } else {
                    if (onCleanPipes != null) {
                        onCleanPipes.cleanPipes(Integer.parseInt(etFluidAmount.getText().toString()), Integer.parseInt(etFluidAmount.getText().toString()), Integer.parseInt(etCleanNumbers.getText().toString()), Integer.parseInt(etLoopTimes.getText().toString()), Integer.parseInt(etFluidPump.getText().toString()), Integer.parseInt(etCirclingPump.getText().toString()), Integer.parseInt(etWastePump.getText().toString()));
                        btnOperate.setText("清洗中...");
                        btnOperate.setEnabled(false);
                        btnStopOperate.setVisibility(View.VISIBLE);
                        etFluidAmount.setEnabled(false);
                        etLoopTimes.setEnabled(false);
                        etCleanNumbers.setEnabled(false);
                    }else {
                        this.dismiss();
                    }
                }
                break;
        }
    }

    private String check() {
        String err = null;
        if (TextUtils.isEmpty(etFluidAmount.getText())) {
            err = "请输入进液体积";
        } else if (TextUtils.isEmpty(etLoopTimes.getText())) {
            err = "请输入循环";
        } else if (TextUtils.isEmpty(etCleanNumbers.getText())) {
            err = "请输入清洗次数";
        }
        return err;
    }

    public OnCleanPipes getOnCleanPipes() {
        return onCleanPipes;
    }

    public void setOnCleanPipes(OnCleanPipes onCleanPipes) {
        this.onCleanPipes = onCleanPipes;
    }
}
